package com.leinao.survey.controller;

import com.alibaba.fastjson.JSON;
import com.leinao.commons.Response;
import com.leinao.constant.Constant;
import com.leinao.constant.ProjectErrorEnum;
import com.leinao.login.dto.LoginResultDto;
import com.leinao.survey.model.UserSurvey;
import com.leinao.survey.service.UserSurveyService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 *
 * @Description 用户反馈调查控制层
 * @author lizi
 * @date 2019/06/21 下午6:48
 * @Version 1.0
 */
@RestController
@CrossOrigin
public class UserSurveyController {

	private static Logger logger = LoggerFactory.getLogger(UserSurveyController.class);

	@Autowired
	private UserSurveyService userSurveyService;

    @ApiOperation(value = "添加用户反馈调查信息", notes = "添加用户反馈调查信息")
    @PostMapping(value = "/userSurvey/add")
    @ApiImplicitParam(name = "userSurvey", value = "userSurvey", required = true, dataType = "userSurvey")
    public Response<UserSurvey> addUserSurvey(@RequestBody UserSurvey userSurvey,
                                             HttpServletRequest request) {
        logger.info("addUserSurvey param  userSurvey: {}", userSurvey);

        // 校验数据
        if(userSurvey.getLevel() == null || StringUtils.isEmpty(userSurvey.getRemark())) {
            return Response.ok(Constant.RESPONSE_STATUS_SUCCESS, ProjectErrorEnum.PARAM_ERROR.getSeq(), ProjectErrorEnum.PARAM_ERROR.getDesc(),null);
        }

        // 获取登录用户信息
        String obj = (String) request.getSession().getAttribute(Constant.USER_SESSION_KEY);
        LoginResultDto userInfo = JSON.parseObject(obj, LoginResultDto.class);

        userSurvey.setUserId(userInfo.getUserId());
        return userSurveyService.addUserSurvey(userSurvey);
    }
}
